<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="1000px" append-to-body custom-class="import-dialog" @close="cancel">
      <div class="container">
        <div class="header">
          <div class="tips">
            <p>1、验房人员需要按位置、检查点填写验房信息</p>
            <p>2、打扫人员需要按位置上传打扫后房间信息</p>
            <p>3、修改后不影响已创建的验房/打扫信息</p>
          </div>
          <div class="handleBtn">
            <el-button @click="handleAddPosition" v-hasPermi="['enterprise:cleaningProcess:add']">新增位置</el-button>
          </div>
        </div>
        <div class="main">
          <div class="table">
            <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" :header-cell-style="{
              background: '#E7E7E7',
              color: '#333',
              fontWeight: 'bold'
            }" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
              <!-- <el-table-column type="selection" width="50" align="center" /> -->
              <el-table-column label="位置" key="weizhi" prop="weizhi" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  <span v-if="!scope.row.parentId">{{ scope.row.name }}</span>
                </template>
              </el-table-column>
              <el-table-column label="检查点" key="name" prop="name">
                <template slot-scope="scope">
                  <span v-if="!!scope.row.parentId">{{ scope.row.name }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <div class="handleTool">
                    <span>
                      <el-button size="mini" type="text" sweepState @click="handleAddCheckpoint(scope.row)"
                        v-show="!scope.row.parentId">新增检查点</el-button>
                    </span>
                    <span><el-button size="mini" type="text" sweepState
                        @click="handleEdit(scope.row)">编辑</el-button></span>
                    <span><el-button size="mini" type="text" sweepState @click="handleMoveUpCleaningProcess(scope.row)"
                        :disabled="scope.row.sort == 1">上移</el-button></span>
                    <span><el-button size="mini" type="text" sweepState @click="handleShiftDownCleaningProcess(scope.row)" :disabled="isButtom(scope.row)">下移</el-button></span>
                    <span><el-button size="mini" type="text" sweepState
                        @click="handleDelete(scope.row)" v-if="!scope.row.children">删除</el-button></span>

                  </div>

                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </el-dialog>
    <editCleaningProcess :type="type" :title="addTitle" v-model="cleaningProcessDialogVisible" :data="cleaningProcessForm"
      @success="getList" />
  </div>
</template>

<script>
import { treeCleaningProcess, getCleaningProcess, deleteCleaningProcess,moveUpCleaningProcess,shiftDownCleaningProcess } from "@/api/houseCleaning/process";

import editCleaningProcess from "./components/editCleaningProcess.vue";
export default {
  components: {
    editCleaningProcess
  },
  props: {
    title: {
      default: "保洁流程",
    },
    value: {
      default: false,
    },
    houseId: {
      default: undefined,
    },
  },
  data() {
    return {
      visible: false,
      estateVisible: false,
      form: {},
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: [],
      //位置点弹窗显示
      cleaningProcessDialogVisible: false,
      addTitle: '',
      cleaningProcessForm: {},
      //添加类型
      type: ''
    };
  },
  computed: {
    isButtom() {
      return (value) => {
        let list = []
        if (value.parentId) {
          let parent = this.list.find(item => item.id == value.parentId)
          list = parent.children
        } else {
          list = this.list
        }
        let index = list.findIndex(item => item.id == value.id)
        return index !== -1 && index === list.length - 1;
      }
    }
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init();
      }
      this.visible = newVal; // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit("input", val); // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    },
  },
  methods: {
    init() {
      this.reset()
      this.getList()
    },
    reset() {
      this.form = {
      }
      this.resetForm("form");
    },
    cancel() {
      this.$emit('success')
      this.reset();
      this.visible = false;
    },
    getList() {
      // this.list = [{
      //   id: 1,
      //   weizhi: '客厅',
      //   jianchadian: '',
      //   parentId: 0,
      //   children: [{
      //     id: 31,
      //     weizhi: '',
      //     jianchadian: '空调',
      //   }, {
      //     id: 32,
      //     weizhi: '',
      //     jianchadian: '冰箱',
      //   }]
      // }, {
      //   id: 2,
      //   weizhi: '主人房',
      //   jianchadian: '',
      //   parentId: 0,
      //   children: [{
      //     id: 33,
      //     weizhi: '',
      //     jianchadian: '床',
      //   }, {
      //     id: 34,
      //     weizhi: '',
      //     jianchadian: '床头柜',
      //   }]
      // }]
      let params = { houseId: this.houseId }
      treeCleaningProcess(params).then(res => {
        this.list = res.data
      })

    },
    handleSelectionChange() {

    },
    //新增保洁流程-位置
    handleAddPosition() {
      this.type = 'position'
      this.addTitle = '新增位置'
      this.cleaningProcessForm = {
        houseId: this.houseId,
        cleaningProcessName: ''
      }
      this.cleaningProcessDialogVisible = true
    },
    handleAddCheckpoint(row) {
      this.type = 'checkpoint'
      this.addTitle = '新增检查点'
      this.cleaningProcessForm = {
        houseId: this.houseId,
        cleaningProcessName: '',
        parentId: row.id
      }
      this.cleaningProcessDialogVisible = true
    },
    //编辑
    handleEdit(row) {
      if (row.parentId) {
        this.type = 'checkpoint'
        this.addTitle = '编辑检查点'
      } else {
        this.type = 'position'
        this.addTitle = '编辑位置'
      }
      getCleaningProcess(row.id).then(res => {
        this.cleaningProcessForm = res.data
        this.cleaningProcessDialogVisible = true
      })
      // this.cleaningProcessForm = Object.assign({}, row)

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.id || this.ids;
      this.$modal.confirm('确定删除数据？').then(function () {
        return deleteCleaningProcess(Ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    // 上移
    handleMoveUpCleaningProcess(row){
      moveUpCleaningProcess(row.id).then(res=>{
        this.getList();
      })
    },
    handleShiftDownCleaningProcess(row){
      shiftDownCleaningProcess(row.id).then(res=>{
        this.getList();
      })
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/dialog.scss";

.header {
  .tips {
    background: #F2F2F2;
    padding: 10px 30px;
  }

  .handleBtn {
    margin: 10px 0
  }
}

.main {
  .handleTool {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
    justify-items: end
  }
}
</style>
